﻿using System;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.InstantTickets
{
    public partial class NewService : System.Web.UI.Page
    {
        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private static CategoryBiz _categoryBiz;
        private static CategoryBiz CategoryManager
        {
            get { return _categoryBiz ?? (_categoryBiz = new CategoryBiz()); }
        }

        private long _ticketId;
        private long TicketId
        {
            get
            {
                if (_ticketId <= 0) long.TryParse(Request.Params["ticketId"], out _ticketId);
                return _ticketId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (TicketId <= 0)
                {
                    lblErrorMessage.Text = Resources.GlobalStrings.InstantTicketNotExisted;
                    btnSave.Enabled = false;
                    return;
                }
                LoadData();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            var serviceId = hidServiceId.Value;
            if (string.IsNullOrEmpty(serviceId))
            {
                lblErrorMessage.Text = Resources.GlobalStrings.ProductIsRequired;
                return;
            }

            var descrition = txtDescription.Text;
            var note = txtNote.Text;

            var ticketService = new InstantTicket_Service
            {
                InstantTicketId = TicketId,
                ServiceId = int.Parse(serviceId),
                Description = descrition,
                Note = note
            };

            int quantity;
            if (int.TryParse(txtQuantity.Text.Trim(), out quantity)) ticketService.Quantity = quantity;

            int serviceTypeId;
            if (int.TryParse(ddlServiceTypes.SelectedValue, out serviceTypeId)) ticketService.ServiceTypeId = serviceTypeId;

            int errorId;
            if (int.TryParse(ddlErrors.SelectedValue, out errorId)) ticketService.ErrorId = errorId;

            // Call Biz Save Product
            InstantTicketManager.CreateTicketService(ticketService);

            Response.Redirect(string.Format("Edit.aspx?id={0}#tabs-1", TicketId));
        }

        private void LoadData()
        {
            // warranty types
            var serviceTypes = CategoryManager.GetAllServiceTypes();
            serviceTypes.Insert(0, new ServiceType { Id = 0, Name = string.Empty });
            ddlServiceTypes.DataTextField = "Name";
            ddlServiceTypes.DataValueField = "Id";
            ddlServiceTypes.DataSource = serviceTypes;
            ddlServiceTypes.DataBind();

            // errors
            var errors = CategoryManager.GetAllErrors();
            errors.Insert(0, new Error { Id = 0, Name = string.Empty });
            ddlErrors.DataTextField = "Name";
            ddlErrors.DataValueField = "Id";
            ddlErrors.DataSource = errors;
            ddlErrors.DataBind();
        }
    }
}